
import { stringify } from 'querystring';

export function getKepperToken():string {
  return localStorage.getItem('keeper-token') || "";
}

export function setKepperToken(token:string):void {
  localStorage.setItem('keeper-token',token)
}

export function getToken(): string {
  return localStorage.getItem('sword-token') || '';
}

export function setToken(token: string): void {
  localStorage.setItem('sword-token', token);
}

export function getAccessToken(): string {
  return localStorage.getItem('sword-access-token') || '';
}

export function setAccessToken(accessToken: string): void {
  localStorage.setItem('sword-access-token', accessToken);
}

export function setRefreshToken(refreshToken: string | undefined): void {
  localStorage.setItem('sword-refresh-token', refreshToken || '');
}

export function getRefreshToken() {
  return localStorage.getItem('sword-refresh-token') || '';
}

export function setSessionExpireTime(time: number): void {
  localStorage.setItem('sword-session-expire-time', String(time));
}
export function getSessionExpireTime() {
  return localStorage.getItem('sword-session-expire-time') || '';
}
export function setTokenCreateTime(time: number): void {
  localStorage.setItem('sword-token-create-time', String(time));
}

export function getTokenCreateTime(): string {
  return localStorage.getItem('sword-token-create-time') || '';
}

export function setTokenExpireTime(time: number): void {
  localStorage.setItem('sword-token-expire-time', String(time));
}

export function getTokenExpireTime(): string {
  return localStorage.getItem('sword-token-expire-time') || '';
}

export function getTopMenus(): [] {
  return JSON.parse(localStorage.getItem('sword-top-menus') || '[]');
}

export function setTopMenus(menus: []): void {
  localStorage.removeItem('sword-top-menus');
  localStorage.setItem('sword-top-menus', JSON.stringify(menus));
}

export function getRoutes(): [] {
  return JSON.parse(localStorage.getItem('sword-routes') || '[]');
}

export function getButtons() {
  return JSON.parse(localStorage.getItem('sword-buttons') || '[]');
}

export function getButton(code: string) {
  const buttons = getButtons();
  const data = buttons.filter((d: { code: string }) => {
    return d.code === code;
  });
  return data.length === 0 ? [] : data[0].children;
}

export function setButtons<T>(buttons: T[]): void {
  localStorage.removeItem('sword-buttons');
  localStorage.setItem('sword-buttons', JSON.stringify(buttons));
}

export function getCurrentUser() {
  return JSON.parse(localStorage.getItem('sword-current-user') || '{}');
}
export function getCurrentUserInfo() {
  return JSON.parse(localStorage.getItem('user_info') || '{}');
}

export function setCurrentUser(account:any) {
  localStorage.setItem('sword-current-user', JSON.stringify(account));
}

export function setCaptchaKey(key: string): void {
  localStorage.removeItem('sword-captcha-key');
  localStorage.setItem('sword-captcha-key', key);
}

export function getCaptchaKey(): string {
  return localStorage.getItem('sword-captcha-key') || '';
}

export function removeAll() {
  localStorage.removeItem('sword-authority');
  localStorage.removeItem('sword-token');
  localStorage.removeItem('sword-top-menus');
  localStorage.removeItem('sword-routes');
  localStorage.removeItem('sword-buttons');
  localStorage.removeItem('sword-current-user');
  localStorage.removeItem('sword-captcha-key');
  localStorage.removeItem('sword-refresh-token');
  localStorage.removeItem('sword-current-user');
  // localStorage.removeItem('sword-token-create-time')
  localStorage.removeItem('sword-token-expire-time');
  localStorage.removeItem('sword-session-expire-time');
  localStorage.removeItem("keeper-token");
  localStorage.removeItem('users');
  localStorage.removeItem("user_info");
  localStorage.removeItem("sword-access-token")
}



